package com.tanhua.server.controller;

import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.UserLikeCountVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.serivce.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID, @RequestHeader("Authorization") String token) {
        //缺少token校验部分
        Long userId = userID;
        if(userId == null) { //如果传入的userID是null，解析token
            userId = UserHolder.getUserId();
        }
        //调用service查询
        UserInfoVo info = usersService.findUserInfoById(userId);
        return ResponseEntity.ok(info);
    }


    /**
     * 更新用户信息
     *   请求地址： put
     *   请求参数： body：UserInfo
     *   请求头： Authorization
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo info,
                                         @RequestHeader("Authorization") String token) {
        Long userId = UserHolder.getUserId();
        //2、设置id
        info.setId(userId);
        //3、调用service
        usersService.updateUserInfo(info);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询通用设置
     *   GET   /settings
     *   返回值：
     *      SettingsVo
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        //1、调用service查询
        SettingsVo vo = usersService.settings();
        //2、构造返回值
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题
     *   POST /questions
     *   参数：body
     *      content
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //1、获取问题内容
        String content = (String) map.get("content");
        //2、调用service
        usersService.questions(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 设置通知开关
     *    POST    /notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map) {
        //调用serivce
        usersService.setting(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单列表
     *   GET   /blacklist
     *   请求参数：
     *     Query参数：
     *        /blacklist?page=1&pagesize=10
     *   响应数据
     *     PageResult --分页VO对象
     *     存储的数据列表：
     *        UserInfo
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10")  Integer pagesize) {
        //1、调用service查询
        PageResult vo = usersService.blacklist(page,pagesize);
        //2、构造返回
        return ResponseEntity.ok(vo);
    }
    /**
     * 移除黑名单
     *   DELETE   /blacklist/{uid}
     *     路径参数
     *     uid ：黑名单的用户id
     */
    @DeleteMapping("/blacklist/{blackUserId}")
    public ResponseEntity delBlacklist(@PathVariable("blackUserId") Long blackUserId) {
        usersService.delBlacklist(blackUserId);
        return ResponseEntity.ok(null);
    }


    /**
     *  互相喜欢、喜欢、粉丝、谁看过我 - 分页列表
     *  请求地址：
     *      GET     /friends/:type
     *                      1 互相关注
     *                      2 我关注
     *                      3 粉丝
     *                      4 谁看过我
     *      参数：page，pagesize
     *      返回值：
     *          PageResult
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") Integer type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10")  Integer pagesize) {
        PageResult vo =  usersService.friends(type,page,pagesize);
        System.out.println(vo);
        return ResponseEntity.ok(vo);
    }

    /**
     * 喜欢取消喜欢数据统计
     *   请求地址： GET  /users/counts
     *   请求头： Authorization
     *   返回值:
     *      eachLoveCount 互相喜欢
     *      loveCount   喜欢
     *      fanCount    粉丝
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        UserLikeCountVo vo = usersService.counts();
        System.out.println(vo);
        return ResponseEntity.ok(vo);
    }

    /**
     * 粉丝 - 喜欢
     *  POST  /users/fans/:uid
     *  参数
     *      uid
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansIsLike(@PathVariable("uid") String uid){
        usersService.fansIsLike(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 喜欢 - 取消
     * DELETE/users/like/:uid
     * uid	用户id
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity IsLike(@PathVariable("uid") String uid){
        usersService.setLike(uid);
        return ResponseEntity.ok(null);
    }



    /**
     * 修改手机号，收验证码
     */
    /**
     * 修改手机号，收验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
//        String code = RandomUtil.randomNumbers(6);
        String phone = UserHolder.getMobile();
        String code="123456";
//        smsTemplate.sendSms(phone,code);
        //将验证吗存入redis
        redisTemplate.opsForValue().set("Code_"+phone,code,5, TimeUnit.MINUTES);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号，效验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        //获取前端输入的验证码
        String verificationCode = (String) map.get("verificationCode");
        //调用service效验验证码
        Boolean checkCode=usersService.checkVerificationCode(verificationCode);
        //获取结果,构建map集合返回给页面
        Map verificationMap=new HashMap();
        verificationMap.put("verification",checkCode);
        return ResponseEntity.ok(verificationMap);
    }
    //修改手机号 - 3 保存
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        //获取到前端发过来的新手机号
        String phone = (String) map.get("phone");
        //将新手机号交给service层处理
        usersService.phone(phone);
        return ResponseEntity.ok(null);
    }
}
